<?php

namespace App\Http\Controllers\order;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils;
use App\Http\Resources\OrderInterest\OrderInterestRecord;
use App\Models\OrderRecord;
use Illuminate\Http\Request;

/*
* @Author: 李明
* @Date: 2021-08-18 10:01:16
 * @LastEditTime: 2022-02-08 09:43:41
 * @LastEditors: 李明
* @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/order/OrderRecordController.php
* @山东壹零贰肆信息科技有限公司
*/

class OrderRecordController extends Controller
{
    /**
     * @description: 订单变化记录
     * @param {Request} $request
     * @return {*}
     */
    public function OrderRecordQuery(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        // $worker_id = $tokenInfo['worker_id'];
        $all = $request->all();

        $where = [];
        if (isset($all['start_time'])) {
            $where[] = ['order_record.create_time', '>=', $all['start_time']];
        }
        if (isset($all['end_time'])) {
            $where[] = ['order_record.create_time', '<=', $all['end_time']];
        }
        if (isset($all['type'])) { // 1是审核 2是订单生成 3是订单修改 4订单转移 5财务 6出纳
            $where[] = ['order_record.type', $all['type']];
        }
        if (isset($all['contract_no'])) {
            $where[] = ["order.contract_no", 'like', '%' . $all['contract_no'] . '%'];
        }
        if (isset($all['user_name'])) {
            $where[] = ["user.name", 'like', '%' . $all['user_name'] . '%'];
        }
        if (isset($all['worker_name'])) {
            $where[] = ["worker.name", 'like', '%' . $all['worker_name'] . '%'];
        }
        $typeList = ['1', '2', '3', '4']; // 1是审核 2是订单生成 3是订单修改 4订单转移 5财务 6出纳
        $pageSize = $all['size'] ?? 15;
        $orderRecordList = OrderRecord::where($where)
            ->whereIn('type', $typeList)
            ->orderBy("update_time", "desc")
            ->paginate($pageSize);
        return LMReturn::Success($orderRecordList);
    }
    /**
     * @description: 订单打款记录
     * @param {Request} $request
     * @return {*}
     */
    public function OrderInterestRecordQuery(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        // $worker_id = $tokenInfo['worker_id'];
        $all = $request->all();

        $where = [];
        if (isset($all['start_time'])) {
            $where[] = ['create_time', '>=', $all['start_time']];
        }
        if (isset($all['end_time'])) {
            $where[] = ['create_time', '<=', $all['end_time']];
        }
        if (isset($all['type'])) { // 1是审核 2是订单生成 3是订单修改 4订单转移 5财务 6出纳
            $where[] = ['type', $all['type']];
        }
        $pageSize = $all['size'] ?? 15;
        $typeList = ['5', '6', '7', '8']; // 5财务 6出纳 7财务批量 8出纳批量
        $orderInterestRecordList = OrderRecord::where($where)
            ->whereIn('type', $typeList)
            ->when(isset($all['user_name']), function ($query) use ($all) {
                $user_name = $all['user_name'];
                return $query->whereHas('order.user', function ($q) use ($user_name) {
                    $q->where("name", 'like', '%' . $user_name . '%');
                });
            })
            ->when(isset($all['worker_name']), function ($query) use ($all) {
                $worker_name = $all['worker_name'];
                return $query->whereHas('worker', function ($q) use ($worker_name) {
                    $q->where("name", 'like', '%' . $worker_name . '%');
                });
            })
            ->when(isset($all['contract_no']), function ($query) use ($all) {
                $contract_no = $all['contract_no'];
                return $query->whereHas('order', function ($q) use ($contract_no) {
                    $q->where("contract_no", 'like', '%' . $contract_no . '%');
                });
            })
            ->orderBy("update_time", "desc")
            ->paginate($pageSize);
        $res_order_list = OrderInterestRecord::collection($orderInterestRecordList);
        $return_datas = Utils::deal_page_resources($res_order_list, $orderInterestRecordList);
        return LMReturn::Success($return_datas);
    }

    /**
     * @description: 插入订单变化记录 和上一个接口区别在于传入的 order 是 对象
     * @param  Order $order  订单对象
     * @param  char $tokenInfo  登录人信息
     * @param  int $type  1是审核 2是订单生成 3是订单修改 4订单转移
     * @param  int $is_success  //  1通过/成功 2拒绝/失败
     */
    public static function InsertOrderModelRecord($order, $tokenInfo, $type, $audit_step = 0, $is_success = '1', $remark = '')
    {
        $remark = $remark == '' ? OrderRecordController::typeToText($type, '1') : $remark;
        //在记录表里面去添加
        // dd($orderInfo);
        $res = OrderRecord::create([
            "worker_id" => $tokenInfo['worker_id'],
            "audit_step" => $audit_step, //审核的步骤   1初审  2复审 0不是审核
            "order_id" => $order->id,
            "type" => $type, // 1是审核 2是订单生成   3是订单修改   4订单转移
            "is_success" => $is_success, //  1通过/成功 2拒绝/失败
            "remark" => $remark
        ]);

        return $res;
    }
    /**
     * @description: 插入订单利息操作记录
     * @param  $orderInfo  订单信息
     * @param  $tokenInfo  登录人信息
     * @param  $type  1是审核 2是订单生成 3是订单修改 4订单转移5财务 6出纳 7财务批量操作 8 出纳批量操作 0直接使用
     */
    public static function intert_order_reocrd_interest_record($interest_record, $tokenInfo, $type, $is_success = '1', $remark = '')
    {
        if ($type == '0') {
            $remark = $remark;
        } else {
            $remark = OrderRecordController::typeToText($type, $is_success);
        }
        //在记录表里面去添加
        $order_record = OrderRecord::create([
            "worker_id" => $tokenInfo['worker_id'],
            "audit_step" => 0, //审核的步骤   1初审  2复审 0不是审核
            "order_id" => $interest_record->order_id,
            "type" => $type, // 1是审核 2是订单生成   3是订单修改   4订单转移  5财务 6出纳
            "ir_id" => $interest_record->id,
            "is_success" => $is_success, //  1通过/成功 2拒绝/失败
            "remark" => $remark
        ]);
        return $order_record;
    }
    public static function typeToText($type, $is_success)
    {
        if ($type == '1') {
            if ($is_success == '1') {
                return '已审核';
            } else {
                return '审核失败';
            }
        } elseif ($type == '2') {
            return '订单生成';
        } elseif ($type == '3') {
            return '订单修改';
        } elseif ($type == '4') {
            return '订单转移';
        } elseif ($type == '5') {
            if ($is_success == '1') {
                return '财务转出纳成功';
            } else {
                return '财务转出纳失败';
            }
        } elseif ($type == '6') {
            if ($is_success == '1') {
                return '出纳成功';
            } else {
                return '出纳失败';
            }
        } elseif ($type == '7') {
            return '财务批量操作';
        } elseif ($type == '8') {
            return '出纳批量操作';
        }
    }
}
